<?php
/**
 * XGPHP 轻量级PHP框架
 * @link http://xgphp.xg3.cn
 * @version 2.0.0
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @author 讯高科技 <xungaokeji@qq.com>
*/
namespace xg;
use \helper\Arr;
use \xg\facade\Config;

class Log {
	protected $config = [
		'default'=>'file',
		'max_days'=>0, // 最大天数 0表示不限制
		'level'=>['system','running','request','error','sql','cache'], // 记录等级 null表示不限制
		'encoding'=>'json', // 数组数据的编码方式
		'channels'=>[
			//通道定义的值会覆盖全局定义的值
			'file'=>[
				'type'=>'File',
				//通道定义 'max_days'=>0, // 最大天数
				//通道定义 'level'=>null, // 记录等级
				'encoding'=>null, // 数组数据的编码方式
				'file_size'=>'1mb', // 文件大小
				'path'=>'xg:runtime/log/', // 文件路径
				'max_files'=>0, // 最大文件数
			]
		]
	];
	public $channel=null;
	protected $level=[];
	protected $logs=[];
	protected $name=null;
	protected static $channels=[];
	protected static $instances=[];
	public static function saveAll(){
		//$runningInfo=self::running();
		foreach(self::$instances as $ins){
			//$ins->log($runningInfo);
			$ins->save();
		}
	}
	public function __construct($config=null){
		if(!$config){
			$config=Config::get('log',[]);
		}
		$this->channel=$this->config($config)->create();
		self::$instances[]=$this;
	}
	//修改配置
	public function config($name,$value=null){
		if(is_array($name)){
			$this->config=Arr::extend($this->config,$name);
		}elseif($name and is_string($name)){
			$this->config[$name]=$value;
		}
		if($this->config['level']){
			$this->level=Arr::split($this->config['level']);
		}else{
			$this->level=[];
		}
		return $this;
	}
	//加载或切换通道
	public function create($channel=null){
		$channel=$channel?$channel:$this->config['default'];
		if(!$this->channels[$channel] and class_exists($class='\\xg\\log\\'.ucwords($channel))) {
			$globalConf=$this->config; // 全局配置
			$channelConf=$this->config['channels'][$channel]; // 通道配置
			// 使用全局保存天数配置
			if(!isset($channelConf['max_days']) and $globalConf['max_days']){
				$channelConf['max_days']=$globalConf['max_days'];
			}
			// 使用全局记录等级配置
			if(!isset($channelConf['level']) and $globalConf['level']){
				$channelConf['level']=Arr::split($globalConf['level']);
			}
			$instance = new $class();
			$instance->config($channelConf);
			return $this->channels[$channel]=$instance;
		}
		throw new Exception(\xg\facade\Lang::get('log channel "{:channel}" not exists',['channel'=>$channel]));
	}
	//记录日志
	public function log($info,$level=null,$type=null){
		$info=$this->format($info,$level,$type);
		if($info){
			if(in_array($info['level'],$this->level)){
				$this->channel->log($info);
			}
		}
		return $this;
	}
	//记录日志
	public function debug($info,$type=null){
		$info=$this->log($info,'debug',$type);
		return $this;
	}
	//实时写入日志
	public function save(){
		if($this->channel){
			$this->channel->save();
		}
	}
	//实时写入日志
	public function write($info,$level=null,$type=null){
		if($this->channel){
			$info=$this->format($info,$level,$type);
			if($info){
				if(in_array($info['level'],$this->level)){
					$this->channel->write($info);
				}
			}
		}
		return $this;
	}
	//返回标准的日志信息
	protected function format($info,$level=null,$type=null){
		if(!is_array($info) or !isset($info['info'])){
			$info=['info'=>$info];
		}
		if($info['info']){
			if(!$info['level']){
				$info['level']=$level?$level:'info';
			}
			if(!$info['type']){
				$info['type']=$type?$type:'info';
			}
			if(!$info['time']){
				$info['time']=microtime(true);
			}
			return $info;
		}
		return null;
	}
	// 获取运行数据
	// protected static function running(){
	// 	$running=xg_running_time('xg-start','xg-save-log');
	// 	$memory=xg_running_memory('xg-start','xg-save-log');
	// 	$included=get_included_files();
	// 	$info=[
	// 		'running'=>$running,
	// 		'memory'=>$memory,
	// 		'included'=>$included
	// 	];
	// 	return ['time'=>microtime(true),'level'=>'system','type'=>'running','info'=>$info];
	// }
	// 获取请求数据
	// protected static function request(){
	// 	$method=$_SERVER['REQUEST_METHOD'];
	// 	$ajax=xg('is.ajax');
	// 	$time=XG_START_TIME;

	// 	$url=xg('fullurl');
	// 	$from=xg('referrer');
	// 	$agent=xg('agent');
	// 	$ip=xg_ip();

	// 	$info=[
	// 		'method'=>$method,
	// 		'ajax'=>$ajax,
	// 		'time'=>$time,
	// 		'url'=>$url,
	// 		'from'=>$from,
	// 		'agent'=>$agent,
	// 		'ip'=>$ip
	// 	];
	// 	return ['time'=>XG_START_TIME,'level'=>'system','type'=>'request','info'=>$info];
	// }
}
?>